
//
//  XHVideoFinishView.m
//  ShowYork
//
//  Created by hxh on 2020/9/7.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(void);
#import "VideoPlayFinishView.h"

@interface VideoPlayFinishView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *messageLb;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *confirmBtn;

@property (nonatomic,copy)doneBlock doneBlock;
@property (nonatomic,copy)doneBlock cancelBlock;

@property (nonatomic,copy)NSString *confirmTitle;
@property (nonatomic,copy)NSString *cancelTitle;
@end

@implementation VideoPlayFinishView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle confirmAciton:(void (^)(void))confirmBlock cancelAction:(void (^)(void))cancelBlock{
    if (self = [super init]) {
        
        self.confirmTitle = confirmTitle;
        self.cancelTitle = cancelTitle;
        
        [self setUI];
        
  
        
        self.titleLb.text = title;
        self.messageLb.text = message;
        [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
        [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
        if (confirmBlock) {
            self.doneBlock = ^{
                confirmBlock();
            };
        }
        if (cancelBlock) {
            self.cancelBlock = ^{
                cancelBlock();
            };
        }
    }
    return self;
}
- (void)setUI{
    
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
    self.frame = CGRectMake(0, 0, maxW, maxH);
    self.backgroundColor = RGBA(0, 0, 0, 0.8);
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"确定退出吗？";
    self.titleLb.textColor = [UIColor whiteColor];
    self.titleLb.font = font(18);
    [self addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-50);
    }];
    
    self.messageLb = [[UILabel alloc]init];
    self.messageLb.text = @"学如逆水行舟，不进则退";
    self.messageLb.textColor = [UIColor whiteColor];
    self.messageLb.font = font(14);
    [self addSubview:self.messageLb];
    
    [self.messageLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.titleLb.mas_bottom).offset(9);
    }];
    
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"是的，退出" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(14);
    self.cancelBtn.backgroundColor = [UIColor whiteColor];
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.layer.cornerRadius = 17;
    self.cancelBtn.layer.masksToBounds = YES;
    [self addSubview:self.cancelBtn];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"继续学" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(14);
    self.confirmBtn.backgroundColor = [RPAppManager themColor];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn.layer.cornerRadius = 17;
    self.confirmBtn.layer.masksToBounds = YES;
    [self addSubview:self.confirmBtn];
    
    
    if(self.confirmTitle.length == 0){
        self.confirmBtn.hidden = YES;
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(120, 34));
            make.top.equalTo(self.messageLb.mas_bottom).offset(26);
        }];
    }else{
        self.confirmBtn.hidden = NO;
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).offset(-70);
            make.size.mas_equalTo(CGSizeMake(120, 34));
            make.top.equalTo(self.messageLb.mas_bottom).offset(26);
        }];
    }
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(70);
        make.size.mas_equalTo(CGSizeMake(120, 34));
        make.centerY.equalTo(self.cancelBtn);
    }];
    

}

- (void)cancelMethod{
    [self dismiss];
    self.cancelBlock();
}
- (void)confirmMethod{
    [self dismiss];
    self.doneBlock();
}

- (void)dismiss{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self removeFromSuperview];
}
- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
@end
